package com.shenfa.dao;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.ibatis.sqlmap.client.SqlMapClient;
import com.shenfa.bean.RuleParam;
import com.shenfa.bean.*;
import com.shenfa.bean.bill.rule.RuleMaster;

/**
 * 规则主表操作，规则管理模块
 * 
 * @author longchensan create by 2011-8-28
 * 
 */
public class RuleDao {

	@Autowired
	private SqlMapClient sqlMapClient;


	@SuppressWarnings("unchecked")
	public List<RuleParam> listRuleByArea(String areaNo) {
		try {
			return sqlMapClient.queryForList(
					"rule_manager.listRuleByArea",areaNo);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 根据传入的树节点id查询对应的gridpanel
	 * 
	 * @author longchensan create by 2011-8-27
	 * 
	 * @param ruleMaster
	 *            [取产品id]
	 * @param skipResults
	 *            [起始条数]
	 * @param maxResults
	 *            [每页条数]
	 * 
	 * @return List<RuleMaster>
	 */
	@SuppressWarnings("unchecked")
	public List<RuleMaster> queryGridList(RuleMaster ruleMaster,
			int skipResults, int maxResults) {
		try {
			return sqlMapClient.queryForList(
					"rule_manager.query_gridPanel", ruleMaster, skipResults,
					maxResults);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<RuleMaster> queryGridListSh(RuleMaster ruleMaster,
			int skipResults, int maxResults) {
		try {
			return sqlMapClient.queryForList(
					"rule_manager.query_gridPanelSh", ruleMaster, skipResults,
					maxResults);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	public int queryCountRmSh(RuleMaster ruleMaster) {
		try {
			return (Integer)sqlMapClient.queryForObject(
					"rule_manager.getCountRmSh", ruleMaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}
	
	/**
	 * 根据传入的树节点id查询对应的gridpanel
	 * 
	 * @author longchensan create by 2011-8-27
	 * 
	 * @param ruleMaster
	 *            [取产品id]       
	 * @return List<RuleMaster>
	 */
	@SuppressWarnings("unchecked")
	public List<RuleMaster> queryGridList(RuleMaster ruleMaster) {
		try {
			return sqlMapClient.queryForList(
					"rule_manager.query_gridPanel", ruleMaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	public int queryCountRm(RuleMaster ruleMaster) {
		try {
			return (Integer)sqlMapClient.queryForObject(
					"rule_manager.getCountRm", ruleMaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return -1;
		}
	}

	
	/**
	 * 插入个性化区域规则
	 * @return	true成功，false失败
	 */
	public boolean addRule(RuleMaster rulemaster) {
		try {
			sqlMapClient.insert("rule_manager.add_ruleMaster", rulemaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	/**
	 * 修改个性化区域规则
	 * @return	true成功，false失败
	 */
	public boolean updateRule(RuleMaster rulemaster) {
		try {
			sqlMapClient.update("rule_manager.edit_ruleMaster", rulemaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 软删规则数据
	 * @return	true成功，false失败
	 */
	public boolean deleteRule(RuleMaster rulemaster) {
		try {
			sqlMapClient.delete("rule_manager.delete_ruleMaster", rulemaster.getRule_no());
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 规则从表查询
	 */
	@SuppressWarnings("unchecked")
	public List<RuleParam> listRuleParamByruluNo(RuleMaster rulemaster) {
		try {
			return 
			sqlMapClient.queryForList(
					"rule_manager.query_rule_Param", rulemaster);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * 规则从表查询
	 */
	@SuppressWarnings("unchecked")
	public List<RuleParam> listRuleParamByID(RuleParam ruleParams) {
		try {
			return 
			sqlMapClient.queryForList(
					"rule_manager.query_rule_Param_by_id", ruleParams);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 修改规则从表
	 */
	public boolean updateRuleParam(RuleParam ruleParam) {
		try {
			sqlMapClient.update("rule_manager.edit_ruleParam", ruleParam);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 增加规则从表
	 */
	public boolean addRuleParam(RuleParam ruleParam) {
		try {
			sqlMapClient.insert("rule_manager.add_ruleParamRes", ruleParam);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 删除规则从表  删除单行
	 */
	public boolean deleteRuleParambyid(RuleParam ruleParam) {
		try {
			sqlMapClient.delete("rule_manager.delete_ruleParam_byid", ruleParam);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}

	/**
	 * 规则从从表查询
	 */
	@SuppressWarnings("unchecked")
	public List<RuleOper> listRuleOperByid(RuleParam ruleParam) {
		try {
			return 
			sqlMapClient.queryForList(
					"rule_manager.query_rule_Oper", ruleParam);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
	/**
	 * 修改规则从从表
	 */
	public boolean updateRuleOper(RuleOper ruleOper) {
		try {
			sqlMapClient.update("rule_manager.edit_ruleOper", ruleOper);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 增加规则从从表
	 */
	public boolean addRuleOper(RuleOper ruleOper) {
		try {
			sqlMapClient.insert("rule_manager.add_ruleOper", ruleOper);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	/**
	 * 删除规则从从表  删除单行
	 */
	public boolean deleteRuleOperbyid(RuleOper ruleOper) {
		try {
			sqlMapClient.delete("rule_manager.delete_ruleOper_byid", ruleOper);
		} catch (SQLException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	public Object findTsbp(String s)
	{
			try {
				
				return sqlMapClient.queryForObject(
						"rule_manager.findTsbp",s);
			} catch (SQLException e) {
				e.printStackTrace();
				return null;
			}
	}
}
